﻿<?xml version="1.0" encoding="utf-8"?><Articles><Article><ArticlesID>628</ArticlesID><Title>[Spider] - Đ&amp;#244;i ch&amp;#250;t về b&amp;#243;c t&amp;#225;ch dữ liệu</Title><Body>&amp;lt;p&amp;gt;Spider l&amp;amp;agrave; g&amp;amp;igrave; ? n&amp;amp;oacute;i n&amp;amp;ocirc;m na n&amp;amp;oacute; l&amp;amp;agrave; 1 con robot chuy&amp;amp;ecirc;n thu thập th&amp;amp;ocirc;ng tin từ c&amp;amp;aacute;c trang ba&amp;amp;oacute; điện tử,website để ph&amp;amp;acirc;n t&amp;amp;iacute;ch b&amp;amp;oacute;c t&amp;amp;aacute;ch dữ liệu để lấy về 1 s&amp;amp;ocirc; n&amp;amp;ocirc;ị dung th&amp;amp;ocirc;ng tin m&amp;amp;agrave; lập tr&amp;amp;igrave;nh vi&amp;amp;ecirc;n config từ trước. &amp;lt;br /&amp;gt;
Vậy với .net lập tr&amp;amp;igrave;nh ra sao ? &amp;lt;br /&amp;gt;
Rất đơn giản. &amp;lt;br /&amp;gt;
Bạn sử dụng ngay lớp HttpWebRequest v&amp;amp;agrave; HttpWebResponse trong NameSpace System.Net bước đ&amp;amp;acirc;u l&amp;amp;agrave; sẽ lấy đươc nội dung cuả to&amp;amp;agrave;n trang web m&amp;amp;ocirc;n b&amp;amp;oacute;c t&amp;amp;aacute;ch&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: csharp;&amp;quot;&amp;gt;
public string GetWebContent(string strLink)
       {
           string strContent = &amp;amp;quot;&amp;amp;quot;;
           try
           {
               WebRequest objWebRequest = WebRequest.Create(strLink);

               objWebRequest.Credentials = CredentialCache.DefaultCredentials;

               WebResponse objWebResponse = objWebRequest.GetResponse();
               Stream receiveStream = objWebResponse.GetResponseStream();
               StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.UTF8);

               strContent = readStream.ReadToEnd();
               objWebResponse.Close();
               readStream.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
           return strContent;
       }
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;h&amp;amp;agrave;m GetWebContent sẽ nh&amp;amp;acirc;n v&amp;amp;agrave;o 1 URL (trang web bạn muốn b&amp;amp;oacute;c t&amp;amp;aacute;ch) &amp;lt;br /&amp;gt;
sau khi c&amp;amp;oacute; được nội dung web rồi (dạng html) bạn bắt đ&amp;amp;acirc;u b&amp;amp;oacute;c t&amp;amp;aacute;ch &amp;lt;br /&amp;gt;
c&amp;amp;aacute;i naỳ đoi hỏi ban phải c&amp;amp;oacute; 1 ch&amp;amp;uacute;t kiến thức về regular expression &amp;lt;br /&amp;gt;
đầu ti&amp;amp;ecirc;n bạn sẽ phải loaị bỏ 1 số k&amp;amp;yacute; tự thừa trong code html &amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
\t cho k&amp;amp;yacute; tự tab (ASCII 0x09) &amp;lt;br /&amp;gt;
\r cho carriage return (0x0D) &amp;lt;br /&amp;gt;
\n cho line feed (0x0A). &amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
ok đ&amp;amp;atilde; gần xong r&amp;amp;ocirc;&amp;amp;igrave; bước ti&amp;amp;ecirc;p theo cũng l&amp;amp;agrave; bươc kh&amp;amp;oacute; nhất: bạn sẽ phải l&amp;amp;ecirc;n trang web bạn muốn b&amp;amp;oacute;c t&amp;amp;aacute;ch để ph&amp;amp;acirc;n t&amp;amp;iacute;ch nội dung html đươc trả về sao cho ph&amp;amp;ugrave; hơp nhất, chuẩn nhất với nội dung bạn mu&amp;amp;ocirc;n lấy ở đ&amp;amp;acirc;y minh demo lấy th&amp;amp;ocirc;ng tin tỷ gi&amp;amp;aacute; h&amp;amp;ocirc;&amp;amp;iacute; đo&amp;amp;aacute;i của ng&amp;amp;acirc;n hang Vietcombank &amp;lt;a target=&amp;quot;_blank&amp;quot; href=&amp;quot;http://www.vietcombank.com.vn/&amp;quot;&amp;gt;http://www.vietcombank.com.vn/&amp;lt;/a&amp;gt; &amp;lt;br /&amp;gt;
sau khi ph&amp;amp;acirc;n tich m&amp;amp;igrave;nh m&amp;amp;igrave;nh c&amp;amp;oacute; k&amp;amp;ecirc;t quả như sau: &amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
&amp;amp;quot;&amp;amp;amp;lt;td class=&amp;amp;amp;quot;code&amp;amp;amp;quot;&amp;amp;amp;gt;(?&amp;amp;amp;lt;ExchangeName&amp;amp;amp;gt;.*?)&amp;amp;amp;lt;/td&amp;amp;amp;gt;&amp;amp;amp;lt;td&amp;amp;amp;gt;(?&amp;amp;amp;lt;ExchangeRate1&amp;amp;amp;gt;.*?)&amp;amp;amp;lt;/td&amp;amp;amp;gt;&amp;amp;amp;lt;td&amp;amp;amp;gt;(?&amp;amp;amp;lt;ExchangeRate2&amp;amp;amp;gt;.*?)&amp;amp;amp;lt;/td&amp;amp;amp;gt;&amp;amp;amp;lt;td&amp;amp;amp;gt;(?&amp;amp;amp;lt;ExchangeRate3&amp;amp;amp;gt;.*?)&amp;amp;amp;lt;/td&amp;amp;amp;gt;&amp;amp;quot;&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: csharp;&amp;quot;&amp;gt;
public void ExchangeRates()
       {
           try
           {
               Match match;
               string USD = &amp;amp;quot;&amp;amp;quot;;
               string JPY = &amp;amp;quot;&amp;amp;quot;;
               string GBP = &amp;amp;quot;&amp;amp;quot;;
               string EUR = &amp;amp;quot;&amp;amp;quot;;
               string AUD = &amp;amp;quot;&amp;amp;quot;;
               string strHTML = this.GetWebContent(&amp;amp;quot;http://www.vietcombank.com.vn/&amp;amp;quot;);
               strHTML = Regex.Replace(strHTML, &amp;amp;quot;\t|\r|\n&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;);
               match = Regex.Match(strHTML,ConfigurationSettings.AppSettings[&amp;amp;quot;RegexVietcombank&amp;amp;quot;].ToString());

               while (match.Success)
               {
                   switch (match.Groups[&amp;amp;quot;ExchangeName&amp;amp;quot;].ToString())
                   {
                       case &amp;amp;quot;USD&amp;amp;quot;:
                           USD = match.Groups[&amp;amp;quot;ExchangeRate3&amp;amp;quot;].ToString();
                           break;
                       case &amp;amp;quot;JPY&amp;amp;quot;:
                           JPY = match.Groups[&amp;amp;quot;ExchangeRate3&amp;amp;quot;].ToString();
                           break;
                       case &amp;amp;quot;GBP&amp;amp;quot;:
                           GBP = match.Groups[&amp;amp;quot;ExchangeRate3&amp;amp;quot;].ToString();
                           break;
                       case &amp;amp;quot;EUR&amp;amp;quot;:
                           EUR = match.Groups[&amp;amp;quot;ExchangeRate3&amp;amp;quot;].ToString();
                           break;
                       case &amp;amp;quot;AUD&amp;amp;quot;:
                           AUD = match.Groups[&amp;amp;quot;ExchangeRate3&amp;amp;quot;].ToString();
                           break;
                   }
                   match = match.NextMatch();
               }
               this.WriteExchageJS(USD, JPY, GBP, EUR, AUD);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;sau khi c&amp;amp;oacute; đưoc k&amp;amp;ecirc;t quả b&amp;amp;oacute;c tach html b&amp;amp;ecirc;n tr&amp;amp;ecirc;n ban ho&amp;amp;agrave;n to&amp;amp;agrave;n c&amp;amp;oacute; thể dễ d&amp;amp;agrave;ng thao t&amp;amp;aacute;c với ch&amp;amp;uacute;ng như Write ra file,Insert Database,Send mail,... &amp;lt;br /&amp;gt;
ở đ&amp;amp;acirc;y minh sẽ Write ra file Javascript băng h&amp;amp;agrave;m WriteExchageJS(USD, JPY, GBP, EUR, AUD);&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: csharp;&amp;quot;&amp;gt;
public void WriteExchageJS(string USD, string JPY, string GBP, string EUR, string AUD)
       {
           try
           {
               string strFile = ConfigurationSettings.AppSettings[&amp;amp;quot;ExchageFolder&amp;amp;quot;].ToString() + @&amp;amp;quot;\&amp;amp;quot; + &amp;amp;quot;Exchange.js&amp;amp;quot;;

               StreamWriter strWrite = new StreamWriter(strFile);
               strWrite.WriteLine(&amp;amp;quot;var vForexs = new Array();&amp;amp;quot;);
               strWrite.WriteLine(&amp;amp;quot;var vCosts = new Array();&amp;amp;quot;);
               strWrite.WriteLine(&amp;amp;quot;vForexs[0]  = 'USD';  vCosts[0]  = '&amp;amp;quot; + USD + &amp;amp;quot;';&amp;amp;quot;);
               strWrite.WriteLine(&amp;amp;quot;vForexs[1]  = 'JPY';  vCosts[1]  = '&amp;amp;quot; + JPY + &amp;amp;quot;';&amp;amp;quot;);
               strWrite.WriteLine(&amp;amp;quot;vForexs[2]  = 'GBP';  vCosts[2]  = '&amp;amp;quot; + GBP + &amp;amp;quot;';&amp;amp;quot;);
               strWrite.WriteLine(&amp;amp;quot;vForexs[3]  = 'EUR';  vCosts[3]  = '&amp;amp;quot; + EUR + &amp;amp;quot;';&amp;amp;quot;);
               strWrite.WriteLine(&amp;amp;quot;vForexs[4]  = 'AUD';  vCosts[4]  = '&amp;amp;quot; + AUD + &amp;amp;quot;';&amp;amp;quot;);
               strWrite.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;với HttpWebRequest v&amp;amp;agrave; HttpWebResponse + th&amp;amp;ecirc;m 1 ch&amp;amp;uacute;t ki&amp;amp;ecirc;n thức về regular expression nưa l&amp;amp;agrave; bạn ho&amp;amp;agrave;n to&amp;amp;agrave;n c&amp;amp;oacute; thể b&amp;amp;oacute;c t&amp;amp;aacute;ch n&amp;amp;ocirc;i dung tư bất cứ trang n&amp;amp;agrave;o bạn mu&amp;amp;ocirc;n. &amp;lt;br /&amp;gt;
Ch&amp;amp;uacute;c bạn thanh c&amp;amp;ocirc;ng. &amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
p/s: project của m&amp;amp;igrave;nh chỉ l&amp;amp;agrave; demo n&amp;amp;ecirc;n minh l&amp;amp;agrave;m tr&amp;amp;ecirc;n winform để th&amp;amp;ocirc;ng tin được cập nhật li&amp;amp;ecirc;n tục bạn vi&amp;amp;ecirc;t lại dươ&amp;amp;iacute; dang Console Build ra file exe rồi đăt Scheduled Tasks trong Windows cho n&amp;amp;oacute; chạy 1 khoảng th&amp;amp;ograve;i gian nhất định (do ban chọn) để dữ li&amp;amp;ecirc;ụ li&amp;amp;ecirc;n tục đưoc cập nhật. &amp;lt;br /&amp;gt;
&amp;lt;br /&amp;gt;
Download Project: &amp;lt;a target=&amp;quot;_blank&amp;quot; href=&amp;quot;http://blog.meotom.net/Uploads/admin/2009/6/15/Client.zip&amp;quot;&amp;gt;http://blog.meotom.net/Uploads/admin/2009/6/15/Client.zip&amp;lt;/a&amp;gt;             		  &amp;lt;span class=&amp;quot;info&amp;quot;&amp;gt;           					 			&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;</Body><CommentEnabled>True</CommentEnabled><ViewCount>0</ViewCount><ReleaseDate>11/23/2009 6:12:15 AM</ReleaseDate><TotalComment>0</TotalComment><IsMedia>False</IsMedia><IsPicture>False</IsPicture><IsVote>True</IsVote><ArticlePassword /><PostVisible>0</PostVisible><ArticlePath>/2009/11/23/[Spider]---Doi-chut-ve-boc-tach-du-lieu-274</ArticlePath><Approved>True</Approved><ImagePath /><CategoryId>28</CategoryId><ArrayCatID>28|25</ArrayCatID></Article></Articles>